﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo.History.Entities;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.ObjectResolutionHelper;
using VitCode.Web.BaseData.Helper;


namespace VitCode.Web.BaseData
{
    public partial class ObjEdit : VcPage
    {
        private PageBuilder m_PageBuilder;
        readonly PermissionManager psManager = new PermissionManager();
        BaseDataManager m_Help = new BaseDataManager();
        public static string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\BaseDataSet.xml"; }
        }

        private string ClassName
        {
            get { return ReturnQueryStringValue("ClassName"); }
        }

        private string TypeName
        {
            get { return ReturnQueryStringValue("TypeName"); }
        }

        private string MenuId
        {
            get { return ReturnQueryStringValue("MenuId"); }
        }

        private string Operate
        {
            get { return ReturnQueryStringValue("Operate"); }
        }

        private string Hidden
        {
            get { return ReturnQueryStringValue("Hidden"); }
        }

        private string KeyFieldValue
        {
            get
            {
                if (ViewState["KeyFieldValue"] == null)
                    return "";
                return ViewState["KeyFieldValue"].ToString();
            }
            set
            {
                lblPageState.Text = value.Length == 0 ? "新增" : "修改";
                ViewState["KeyFieldValue"] = value;
            }
        }

        private void SetData()
        {
            if (KeyFieldValue.Length > 0)
                m_PageBuilder.SetPageData(ClassName, TypeName, KeyFieldValue);
            if(Hidden.Trim().Length > 0 || Hidden.Trim() == "Yes")
            {
                NavigateCtl1.Visible = false;
                tbOperater.Visible = false;
            }
        }

        private void SetTabCtlItem()
        {
            pgMenu.Items.Clear();
          
            if (Operate != "Add")
            {
                pgMenu.Items.Add(new MenuItem("基本信息", "0") { Selected = true });
                pgMenu.Items.Add(new MenuItem("历史信息", "1"));
            }
        }


        protected override void OnInit(EventArgs e)
        {
            if (m_PageBuilder == null)
                m_PageBuilder = new PageBuilder(this,SettingXmlPath);
            NavigateCtl1.SettingXmlPath = SettingXmlPath;
            if (!string.IsNullOrEmpty(TypeName))
                m_PageBuilder.SetPageInfoContext(table1, ClassName, TypeName);
            else
                m_PageBuilder.SetPageInfoContext(table1, ClassName);

                SetAssetInfoCtlSetting();
                ctlHisInfo.PrintDataGrid();
                
            base.OnInit(e);
        }

        private string ReturnQueryStringValue(string keyString)
        {
            string strValue = Request.QueryString[keyString];
            if (strValue == null)
                strValue = "";
            return Convert.ToString(strValue);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (!IsPostBack)
            {
                string strKeyField = m_PageBuilder.GetPageKeyField(ClassName);
                KeyFieldValue = ReturnQueryStringValue(strKeyField);
                SetTabCtlItem();
                SetPageTab("0");
                SetData();
                if (!psManager.CheckPageIsCanEdit(this))
                {
                    btnAdd.Enabled = false;
                    btnEnter.Enabled = false;
                    new CreateControlHelper().SetPageControlsDisabled(this);
                }
                if (Operate == "Add")
                    btnAdd.Visible = false;
                Session.Add("Operator",Operate); 
            }
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            var saveHelper = new WebSaveHelper(this,SettingXmlPath);
            object obj;
            StringCollection scMsg;
            obj = saveHelper.SavePageObject(ClassName, TypeName, KeyFieldValue, out scMsg);

            if (scMsg.Count > 0)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < scMsg.Count; i++)
                    sb.Append(scMsg[i] + "\r\n");
                  Alert.MessageBox(sb.ToString());
            }
            else
            {
               
                string strKeyField = m_PageBuilder.GetPageKeyField(ClassName);
                PropertyInfo pi = obj.GetType().GetProperty(strKeyField);
                KeyFieldValue = pi.GetValue(obj, null).ToString();
                Alert.MessageBox("保存成功");
                SetData();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ObjList.aspx?MenuId=" + MenuId + "&ClassName=" + ClassName + "&TypeName=" + TypeName);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            KeyFieldValue = "";
            SetData();
            string editUrl = "ObjEdit.aspx?MenuId=" + MenuId + "&ClassName=" + ClassName;
            if (TypeName.Length > 0)
                editUrl += "&TypeName=" + TypeName;
            editUrl += "&Operate=Add";
            Response.Redirect(Server.UrlPathEncode(editUrl));
           
        }

        private void SetPageTab(string itemVal)
        {
            divBaseInfo.Visible=false;
            divHisInfo.Visible=false;
            if (itemVal == "0")
                divBaseInfo.Visible = true;
            else if (itemVal == "1")
            {
                divHisInfo.Visible = true;
                SetAssetInfoCtlSetting();
                ctlHisInfo.BindData();
            } 
        }

        private void SetAssetInfoCtlSetting()
        {
            ctlHisInfo.ClassName = ClassName + "Record";
            ctlHisInfo.KeyProName = HistoryKeyInfo.H_BASE_OBJECT_ID;
            ctlHisInfo.HiddenColInfo = HistoryKeyInfo.H_ID + "," + HistoryKeyInfo.H_BASE_OBJECT_ID;
            ctlHisInfo.KeyProVal = KeyFieldValue;
            
        }


        protected void pgMenu_MenuItemClick(object sender, System.Web.UI.WebControls.MenuEventArgs e)
        {
            SetPageTab(e.Item.Value);
        }
    }
}